package com.chainyoung.trademarket.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.support.v4.app.NotificationCompat;

import com.chainyoung.common.R;
import com.chainyoung.trademarket.constant.NotificationChannels;

import static android.content.Context.NOTIFICATION_SERVICE;

public class NotificationUtils {

    private NotificationManager mManager;

    private static class SingletonHolder {
        private static final NotificationUtils INSTANCE = new NotificationUtils();
    }

    //获取单例
    public static NotificationUtils getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void createNotificationChannel(Context context, String channelId, String channelName) {
        NotificationChannel channel = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            getManager(context).createNotificationChannel(channel);
        }

    }

    private NotificationManager getManager(Context context) {
        if (mManager == null) {
            mManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    public NotificationCompat.Builder getChannelNotification(Context context, String title, String content, PendingIntent pendingIntent, String channelId, boolean isFloating) {
        if (isFloating) {
            return new NotificationCompat.Builder(context, channelId)
                    .setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_logo)
                    .setFullScreenIntent(pendingIntent, false)
                    .setContentTitle(title)
                    .setContentText(content);
        } else {
            return new NotificationCompat.Builder(context, channelId)
                    .setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_logo)
                    .setContentIntent(pendingIntent)
                    .setContentTitle(title)
                    .setContentText(content);
        }

    }


    public void sendNotification(Context context, String title, String content, PendingIntent pendingIntent, int notifyId, NotificationChannels channel, Boolean isFloating) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            createNotificationChannel(context, channel.channelId, channel.channelName);
            Notification notification = getChannelNotification
                    (context, title, content, pendingIntent, channel.channelId, isFloating).build();
            getManager(context).notify(notifyId, notification);
        } else {
            Notification notification = getChannelNotification(context, title, content, null, null, isFloating).build();
            getManager(context).notify(notifyId, notification);
        }
    }

    public void cancelNotification(Context context, int notifyId) {
        getManager(context).cancel(notifyId);
    }

}
